package com.xindaoapp.happypet.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Banzhu;
import com.xindaoapp.happypet.bean.ForumInfo;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.ProgressHUD;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 我的圈子详情
 * 
 * @author Yangqing
 * 
 */
public class MyClubDetailsActivity extends Activity implements OnClickListener {

    private static final int NUMCOLUMS = 4;// 版主和达人的列数
    private ForumInfo mForumInfo;// 论坛信息
    private TextView tiezi_count, group_count, tv_quanname, tv_tuangroup;// 帖子，成员数量,圈子描述
    private GridView quan_banzhu;// 版主头像
    private GridView quanzi_daren;// 圈子达人
    private CircleImageView quanzi_icon;
    private ImageView iv_addquanzi;// 移除圈子
    private CircleImageView banzhu_icon;
    private TextView banzhu_name;// 版主名字
    private DarenAdapter darenAdapter;
    private ImageButton cancle, post;// 取消、发送帖子
    private TextView tv_title;
    private String name;
    private List<Banzhu> banzhuList = new ArrayList<Banzhu>();// 版主集合
    private List<Banzhu> darenList = new ArrayList<Banzhu>();// 达人集合
    private TextView tv_bangui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myclubdetails);
        mForumInfo = (ForumInfo) getIntent().getSerializableExtra("mForumInfo");
        name = getIntent().getStringExtra("name");
        if (mForumInfo.darenarray != null && mForumInfo.darenarray.size() > 0)
            darenList = mForumInfo.darenarray;
        if (mForumInfo.banzhuarray != null && mForumInfo.banzhuarray.size() > 0)
            banzhuList = mForumInfo.banzhuarray;
        initView();
        initEvents();
        initData();
    }



    private void initView() {
        cancle = (ImageButton) findViewById(R.id.cancel);
        tv_title = (TextView) findViewById(R.id.check_club);
        tv_title.setText(name.replace("俱乐部", ""));
        quanzi_icon = (CircleImageView) findViewById(R.id.quanzi_icon);
        tv_quanname = (TextView) findViewById(R.id.tv_quanname);
        tv_tuangroup = (TextView) findViewById(R.id.tv_tuangroup);
        tv_tuangroup.setVisibility(View.GONE);
        iv_addquanzi = (ImageView) findViewById(R.id.iv_addquanzi);
        iv_addquanzi.setBackgroundResource(R.drawable.post_delete);

        tv_bangui = (TextView) findViewById(R.id.tv_bangui);
        findViewById(R.id.ll_ll).setVisibility(View.GONE);
        post = (ImageButton) findViewById(R.id.publish);

        tiezi_count = (TextView) findViewById(R.id.tiezi_count);
        group_count = (TextView) findViewById(R.id.group_count);
        quan_banzhu = (GridView) findViewById(R.id.quan_banzhu);
        quanzi_daren = (GridView) findViewById(R.id.quanzi_daren);

        BanzhuAdapter banzhuAdapter = new BanzhuAdapter();
        quan_banzhu.setNumColumns(NUMCOLUMS);
        quan_banzhu.setAdapter(banzhuAdapter);
        darenAdapter = new DarenAdapter();
        quanzi_daren.setNumColumns(NUMCOLUMS);
        quanzi_daren.setAdapter(darenAdapter);
    }

    private void initEvents() {
        cancle.setOnClickListener(this);
        post.setOnClickListener(this);
        iv_addquanzi.setOnClickListener(this);
    }

    private void initData() {

        ImageLoader.getInstance().displayImage(mForumInfo.ico, quanzi_icon);
        tv_quanname.setText(mForumInfo.description);
        tiezi_count.setText(mForumInfo.posts);
        tv_bangui.setText(mForumInfo.apprules);
        group_count.setText(mForumInfo.membernum);
        // quan_banzhu.removeAllViews();
        // for (Banzhuarray banzhuInfo : mForumInfo.banzhuarray) {
        // View addBanzhuIcon = addBanzhuIcon();
        // ImageLoader.getInstance().displayImage(banzhuInfo.userface, banzhu_icon);
        // banzhu_name.setText(banzhuInfo.username);
        // quan_banzhu.addView(addBanzhuIcon);
        // }


    }

    /**
     * @author Yangqing 版主头像
     */
    public class BanzhuAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return banzhuList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(MyClubDetailsActivity.this, R.layout.item_banzhuicon, null);
            CircleImageView gv_banzhu_icon =
                    (CircleImageView) convertView.findViewById(R.id.banzhu_icon);
            TextView gv_banzhu_name = (TextView) convertView.findViewById(R.id.banzhu_name);

            ImageLoader.getInstance().displayImage(banzhuList.get(position).userface,
                    gv_banzhu_icon);
            gv_banzhu_name.setText(banzhuList.get(position).username);
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return banzhuList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }



    }


    /**
     * @author Yangqing 达人头像
     */
    public class DarenAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return darenList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(MyClubDetailsActivity.this, R.layout.item_banzhuicon, null);
            CircleImageView gv_banzhu_icon =
                    (CircleImageView) convertView.findViewById(R.id.banzhu_icon);
            TextView gv_banzhu_name = (TextView) convertView.findViewById(R.id.banzhu_name);

            ImageLoader.getInstance()
                    .displayImage(darenList.get(position).userface, gv_banzhu_icon);
            gv_banzhu_name.setText(darenList.get(position).username);
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return darenList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.publish:
                if (UserState.isLogged(this)) {
                    // Intent intent = new Intent(getBaseContext(), PostMessageActivity.class);
                    // intent.putExtra(Constants.KEY_FID, mForumInfo.fid);
                    // intent.putExtra(Constants.KEY_NAME, mForumInfo.name);
                    // startActivity(intent);

                    Intent intent = new Intent(getBaseContext(), PostMessageActivity.class);
                    intent.putExtra(Constants.KEY_GROUP_NAME, mForumInfo.name);
                    intent.putExtra(Constants.KEY_GROUP_MEMBER, mForumInfo.membernum);
                    intent.putExtra(Constants.KEY_GROUP_ICON, mForumInfo.ico);
                    intent.putExtra(Constants.KEY_GROUP_FID, mForumInfo.fid);
                    startActivityForResult(intent, Constants.REQUESTCODE_POSTED);

                    // 添加动画
                    // overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
                    overridePendingTransition(R.anim.new_dync_in_from_right,
                            R.anim.new_dync_out_to_left);
                }
                break;
            case R.id.iv_addquanzi:// 取消加入圈子
                if (Constants.ISMYGROUP) {
                    if (UserState.isLogged(MyClubDetailsActivity.this)) {
                        if (UserState.isLogged(MyClubDetailsActivity.this)) {
                            final ProgressHUD ProgressDialog =
                                    ProgressHUD.show(MyClubDetailsActivity.this, "正在加载...", true,
                                            true, null);
                            HappyPetApplication
                                    .get()
                                    .getMoccaApi()
                                    .outGroup(mForumInfo.fid, MyClubDetailsActivity.this,
                                            new IRequest<Boolean>() {
                                                @Override
                                                public void request(Boolean t) {
                                                    if (null != ProgressDialog
                                                            && ProgressDialog.isShowing()) {
                                                        ProgressDialog.dismiss();
                                                    }
                                                    if (t) {
                                                        Constants.ISMYGROUP = false;
                                                        iv_addquanzi
                                                                .setBackgroundResource(R.drawable.post_add);
                                                    } else {
                                                        iv_addquanzi
                                                                .setBackgroundResource(R.drawable.post_delete);
                                                    }
                                                    sendBroadcast(new Intent(
                                                            Constants.JOIN_OUT_GROUP));
                                                }
                                            });
                        } else {
                            Toast.makeText(getApplicationContext(), R.string.pLease_check_network,
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                } else if (!Constants.ISMYGROUP) {
                    if (CommonUtil.checkNetState(MyClubDetailsActivity.this)) {
                        final ProgressHUD ProgressDialog =
                                ProgressHUD.show(MyClubDetailsActivity.this, "正在加载...", true, true,
                                        null);
                        HappyPetApplication
                                .get()
                                .getMoccaApi()
                                .joinGroup(mForumInfo.fid, MyClubDetailsActivity.this,
                                        new IRequest<Boolean>() {
                                            @Override
                                            public void request(Boolean t) {
                                                if (null != ProgressDialog
                                                        && ProgressDialog.isShowing()) {
                                                    ProgressDialog.dismiss();
                                                }
                                                if (t) {
                                                    Constants.ISMYGROUP = true;
                                                    iv_addquanzi
                                                            .setBackgroundResource(R.drawable.post_delete);
                                                } else {
                                                    iv_addquanzi
                                                            .setBackgroundResource(R.drawable.post_add);
                                                }
                                                sendBroadcast(new Intent(Constants.JOIN_OUT_GROUP));
                                            }
                                        });
                    } else {
                        Toast.makeText(getApplicationContext(), R.string.pLease_check_network,
                                Toast.LENGTH_SHORT).show();
                    }
                }

                break;

        }
    }


}
